<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- qtquicklayouts-overview.qdoc -->
  <title>Qt Quick Layouts Overview | Qt Quick 5.12.3</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.12</a></td><td ><a href="qtquick-index.html">Qt Quick 模块</a></td><td >Qt Quick Layouts Overview</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtquick-index.html">Qt 5.12.3 参考指南</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">目录</a></h3>
<ul>
<li class="level1"><a href="#getting-started">Getting Started</a></li>
<li class="level1"><a href="#key-features">Key Features</a></li>
<li class="level1"><a href="#a-simple-layout">A Simple Layout</a></li>
<li class="level2"><a href="#size-constraints">Size Constraints</a></li>
<li class="level2"><a href="#specifying-preferred-size">Specifying Preferred Size</a></li>
<li class="level1"><a href="#connecting-windows-and-layouts">Connecting Windows and Layouts</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Qt Quick Layouts Overview</h1>
<span class="subtitle"></span>
<!-- $$$qtquicklayouts-overview.html-description -->
<div class="descr"> <a name="details"></a>
<p>Qt Quick Layouts are items that are used to arrange items in a user interface. Since Qt Quick Layouts also resize their items, they are well suited for resizable user interfaces.</p>
<a name="getting-started"></a>
<h2 id="getting-started">Getting Started</h2>
<p>The QML types can be imported into your application using the following import statement in your <code>.qml</code> file.</p>
<pre class="cpp">

  import <span class="type"><a href="qtquick-module.html">QtQuick</a></span><span class="operator">.</span>Layouts <span class="number">1.11</span>

</pre>
<a name="key-features"></a>
<h2 id="key-features">Key Features</h2>
<p>Some of the key features are:</p>
<ul>
<li><a href="qml-qtquick-layouts-layout.html#alignment-attached-prop">Alignment</a> of items can be specified with the <a href="qml-qtquick-layouts-layout.html#alignment-attached-prop">Layout.alignment</a> property</li>
<li><a href="qml-qtquick-layouts-layout.html#fillWidth-attached-prop">Resizable items</a> can be specified with the <a href="qml-qtquick-layouts-layout.html#fillWidth-attached-prop">Layout.fillWidth</a> and <a href="qml-qtquick-layouts-layout.html#fillHeight-attached-prop">Layout.fillHeight</a> properties.</li>
<li><a href="qtquicklayouts-overview.html#size-constraints">Size constraints</a> can be specified with <a href="qml-qtquick-layouts-layout.html#minimumWidth-attached-prop">Layout.minimumWidth</a>, <a href="qml-qtquick-layouts-layout.html#preferredWidth-attached-prop">Layout.preferredWidth</a>, and <a href="qml-qtquick-layouts-layout.html#maximumWidth-attached-prop">Layout.maximumWidth</a> properties (&quot;Width&quot; can be replaced with &quot;Height&quot; for specifying similar constraints to the height).</li>
<li><a href="qml-qtquick-layouts-rowlayout.html#spacing-prop">Spacings</a> can be specified with <a href="qml-qtquick-layouts-rowlayout.html#spacing-prop">spacing</a>, <a href="qml-qtquick-layouts-gridlayout.html#rowSpacing-prop">rowSpacing</a> or <a href="qml-qtquick-layouts-gridlayout.html#columnSpacing-prop">columnSpacing</a></li>
</ul>
<p>In addition to the above features, <a href="qml-qtquick-layouts-gridlayout.html">GridLayout</a> adds these features:</p>
<ul>
<li><a href="qml-qtquick-layouts-layout.html#row-attached-prop">Grid coordinates</a> can be specified with the <a href="qml-qtquick-layouts-layout.html#row-attached-prop">Layout.row</a> and <a href="qml-qtquick-layouts-layout.html#column-attached-prop">Layout.column</a> properties.</li>
<li><a href="qml-qtquick-layouts-gridlayout.html#flow-prop">Automatic grid coordinates</a> used together with the <a href="qml-qtquick-layouts-gridlayout.html#flow-prop">flow</a>, <a href="qml-qtquick-layouts-gridlayout.html#rows-prop">rows</a>, and <a href="qml-qtquick-layouts-gridlayout.html#columns-prop">columns</a> properties.</li>
<li><a href="qml-qtquick-layouts-layout.html#columnSpan-attached-prop">Spans</a> across rows or columns can be specified with the <a href="qml-qtquick-layouts-layout.html#rowSpan-attached-prop">Layout.rowSpan</a> and <a href="qml-qtquick-layouts-layout.html#columnSpan-attached-prop">Layout.columnSpan</a> properties.</li>
</ul>
<a name="a-simple-layout"></a>
<h2 id="a-simple-layout">A Simple Layout</h2>
<pre class="qml">

  <span class="type"><a href="qml-qtquick-window-window.html">Window</a></span> {
      <span class="type"><a href="qml-qtquick-layouts-rowlayout.html">RowLayout</a></span> {
          <span class="name">anchors</span>.fill: <span class="name">parent</span>
          <span class="name">spacing</span>: <span class="number">6</span>
          <span class="type"><a href="qml-qtquick-rectangle.html">Rectangle</a></span> {
              <span class="name">color</span>: <span class="string">'azure'</span>
              <span class="name">Layout</span>.preferredWidth: <span class="number">100</span>
              <span class="name">Layout</span>.preferredHeight: <span class="number">150</span>
          }
          <span class="type"><a href="qml-qtquick-rectangle.html">Rectangle</a></span> {
              <span class="name">color</span>: <span class="string">&quot;plum&quot;</span>
              <span class="name">Layout</span>.fillWidth: <span class="number">true</span>
              <span class="name">Layout</span>.fillHeight: <span class="number">true</span>
          }
      }
  }

</pre>
<p>As the intention of using a layout is to rearrange its children whenever the layout changes size, the application should make sure that the layout gets resized. In the above snippet the <a href="qml-qtquick-layouts-rowlayout.html">RowLayout</a> ensures that by specifying <code>anchors.fill: parent</code>. However, it can also be by other means, such as directly specifying <a href="qml-qtquick-item.html#width-prop">width</a> and <a href="qml-qtquick-item.html#height-prop">height</a> properties. In the same snippet, the <code>azure</code> Rectangle has a fixed size of <code>(100, 150)</code> pixels, and the <code>plum</code> Rectangle will expand to occupy all the space it gets allocated.</p>
<p><b>Note: </b>A layout is responsible for its children's geometry. You should therefore not specify <a href="qml-qtquick-item.html#width-prop">width</a>, <a href="qml-qtquick-item.html#height-prop">height</a>, <a href="qml-qtquick-item.html#x-prop">x</a>, <a href="qml-qtquick-item.html#y-prop">y</a> or any other properties that might influence those properties (such as <a href="qml-qtquick-item.html#anchors-prop">anchors</a>) on those items. Otherwise there would be a conflict of interest, and the result is undefined. This is also the case if the child item is a layout. Therefore, only layouts with no parent layout can have <code>anchors.fill: parent</code>.</p><p>All items in the layout will have 6 pixels of spacing between them:</p>
<pre class="qml">

  <span class="name">spacing</span>: <span class="number">6</span>

</pre>
<a name="size-constraints"></a>
<h3 >Size Constraints</h3>
<p>Since an item can be resized by its layout, the layout needs to know the <a href="qml-qtquick-layouts-layout.html#minimumWidth-attached-prop">minimum</a>, <a href="qml-qtquick-layouts-layout.html#preferredWidth-attached-prop">preferred</a>, and <a href="qml-qtquick-layouts-layout.html#maximumWidth-attached-prop">maximum</a> sizes of all items where <a href="qml-qtquick-layouts-layout.html#fillWidth-attached-prop">Layout.fillWidth</a> or <a href="qml-qtquick-layouts-layout.html#fillHeight-attached-prop">Layout.fillHeight</a> is set to <code>true</code>. For instance, the following will produce a layout with two rectangles lying side-by-side that stretches horizontally. The azure rectangle can be resized from 50x150 to 300x150, and the plum rectangle can be resized from 100x100 to ∞x100.</p>
<pre class="qml">

  <span class="type"><a href="qml-qtquick-layouts-rowlayout.html">RowLayout</a></span> {
      <span class="name">id</span>: <span class="name">layout</span>
      <span class="name">anchors</span>.fill: <span class="name">parent</span>
      <span class="name">spacing</span>: <span class="number">6</span>
      <span class="type"><a href="qml-qtquick-rectangle.html">Rectangle</a></span> {
          <span class="name">color</span>: <span class="string">'azure'</span>
          <span class="name">Layout</span>.fillWidth: <span class="number">true</span>
          <span class="name">Layout</span>.minimumWidth: <span class="number">50</span>
          <span class="name">Layout</span>.preferredWidth: <span class="number">100</span>
          <span class="name">Layout</span>.maximumWidth: <span class="number">300</span>
          <span class="name">Layout</span>.minimumHeight: <span class="number">150</span>
          <span class="type"><a href="qml-qtquick-text.html">Text</a></span> {
              <span class="name">anchors</span>.centerIn: <span class="name">parent</span>
              <span class="name">text</span>: <span class="name">parent</span>.<span class="name">width</span> <span class="operator">+</span> <span class="string">'x'</span> <span class="operator">+</span> <span class="name">parent</span>.<span class="name">height</span>
          }
      }
      <span class="type"><a href="qml-qtquick-rectangle.html">Rectangle</a></span> {
          <span class="name">color</span>: <span class="string">'plum'</span>
          <span class="name">Layout</span>.fillWidth: <span class="number">true</span>
          <span class="name">Layout</span>.minimumWidth: <span class="number">100</span>
          <span class="name">Layout</span>.preferredWidth: <span class="number">200</span>
          <span class="name">Layout</span>.preferredHeight: <span class="number">100</span>
          <span class="type"><a href="qml-qtquick-text.html">Text</a></span> {
              <span class="name">anchors</span>.centerIn: <span class="name">parent</span>
              <span class="name">text</span>: <span class="name">parent</span>.<span class="name">width</span> <span class="operator">+</span> <span class="string">'x'</span> <span class="operator">+</span> <span class="name">parent</span>.<span class="name">height</span>
          }
      }
  }

</pre>
<p class="centerAlign"><img src="images/rowlayout-minimum.png" alt="&quot;RowLayout at its minimum&quot;" /></p><p>Combining each item's constraints will give these implicit constraints to the layout element:</p>
<div class="table"><table class="generic">
 <thead><tr class="qt-style"><th ></th><th >minimum</th><th >preferred</th><th >maximum</th></tr></thead>
<tr valign="top" class="odd"><td >implicit constraints (width)</td><td >156</td><td >306</td><td >∞ (<code>Number.POSITIVE_INFINITY</code>)</td></tr>
<tr valign="top" class="even"><td >implicit constraints (heights)</td><td >150</td><td >150</td><td >150</td></tr>
</table></div>
<p>Thus, the layout cannot be narrower than 156 or be taller or shorter than 150 without breaking any of the constraints of its child items.</p>
<a name="specifying-preferred-size"></a>
<h3 >Specifying Preferred Size</h3>
<p>For each item, the effective preferred size may come from one of several candidate properties. For determining the effective preferred size, it will query these candidate properties in the following order, and use the first candidate with a valid width or height.</p>
<div class="table"><table class="generic">
 <thead><tr class="qt-style"><th >Candidate properties</th><th >Description</th></tr></thead>
<tr valign="top" class="odd"><td ><a href="qml-qtquick-layouts-layout.html#preferredWidth-attached-prop">Layout.preferredWidth</a> or <a href="qml-qtquick-layouts-layout.html#preferredHeight-attached-prop">Layout.preferredHeight</a></td><td >These properties are supposed to be modified by the application if the default implicit size does not give the optimal arrangement.</td></tr>
<tr valign="top" class="even"><td ><a href="qml-qtquick-item.html#implicitWidth-prop">implicitWidth</a> or <a href="qml-qtquick-item.html#implicitHeight-prop">implicitHeight</a></td><td >These properties are supposed to be supplied by each item to give a meaningful ideal size, for example the size needed to display all the contents of a <a href="qml-qtquick-text.html">Text</a> type. An implicit width or height of <code>0</code> is interpreted as invalid.</td></tr>
<tr valign="top" class="odd"><td ><a href="qml-qtquick-item.html#width-prop">width</a> and <a href="qml-qtquick-item.html#height-prop">height</a></td><td >If none of the above properties are valid, the layout will resort to the <a href="qml-qtquick-item.html#width-prop">width</a> and <a href="qml-qtquick-item.html#height-prop">height</a> properties.</td></tr>
</table></div>
<p>An item can specify <a href="qml-qtquick-layouts-layout.html#preferredWidth-attached-prop">Layout.preferredWidth</a> without having to specify <a href="qml-qtquick-layouts-layout.html#preferredHeight-attached-prop">Layout.preferredHeight</a>. In this case, the effective preferred height will be determined from the <a href="qml-qtquick-item.html#implicitHeight-prop">implicitHeight</a> (or ultimately <a href="qml-qtquick-item.html#height-prop">height</a>).</p>
<p><b>Note: </b>Resorting to <a href="qml-qtquick-item.html#width-prop">width</a> or <a href="qml-qtquick-item.html#height-prop">height</a> properties is only provided as a final fallback. If you want to override the preferred size, it is recommended to use <a href="qml-qtquick-layouts-layout.html#preferredWidth-attached-prop">Layout.preferredWidth</a> or <a href="qml-qtquick-layouts-layout.html#preferredHeight-attached-prop">Layout.preferredHeight</a>. Relying on the <a href="qml-qtquick-item.html#width-prop">width</a> or <a href="qml-qtquick-item.html#height-prop">height</a> properties for specifying the preferred size might give some unexpected behavior. For instance, changing the <a href="qml-qtquick-item.html#width-prop">width</a> or <a href="qml-qtquick-item.html#height-prop">height</a> properties won't trigger a layout rearrangement. Also, when the layout is forced to do a full rebuild it might use the actual width and height, and not the width and height specified in the QML file.</p><a name="connecting-windows-and-layouts"></a>
<h2 id="connecting-windows-and-layouts">Connecting Windows and Layouts</h2>
<p>You can just use normal anchoring concepts to ensure that the layout will follow the window resizing.</p>
<pre class="qml">

  <span class="type"><a href="qml-qtquick-layouts-rowlayout.html">RowLayout</a></span> {
      <span class="name">id</span>: <span class="name">layout</span>
      <span class="name">anchors</span>.fill: <span class="name">parent</span>

</pre>
<p>The size constraints of layouts can be used to ensure that the window cannot be resized beyond the layout constraints. You can take the size constraints from the layout and set these constraints on the minimumWidth, minimumHeight, maximumWidth, and maximumHeight of the Window element. The following code ensures that the window cannot be resized beyond the constraints of the layout:</p>
<pre class="qml">

  <span class="name">minimumWidth</span>: <span class="name">layout</span>.<span class="name">Layout</span>.<span class="name">minimumWidth</span>
  <span class="name">minimumHeight</span>: <span class="name">layout</span>.<span class="name">Layout</span>.<span class="name">minimumHeight</span>
  <span class="name">maximumWidth</span>: <span class="number">1000</span>
  <span class="name">maximumHeight</span>: <span class="name">layout</span>.<span class="name">Layout</span>.<span class="name">maximumHeight</span>

</pre>
<p><b>Note: </b>Since layout.Layout.maximumWidth is infinite in this case, we cannot bind that to the maximumWidth property of Window, since that is an integer number. We therefore set a fixed maximum width to 1000.</p><p>Finally, you usually want the initial size of the window to be the layout's implicit size:</p>
<pre class="qml">

  <span class="name">width</span>: <span class="name">layout</span>.<span class="name">implicitWidth</span>
  <span class="name">height</span>: <span class="name">layout</span>.<span class="name">implicitHeight</span>

</pre>
</div>
<!-- @@@qtquicklayouts-overview.html -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
